<template>
  <section class="todoapp">
    <!-- 头部部分 -->
    <ListHeader @addList="addList"></ListHeader>

    <!-- 主体部分 -->
    <ListBody
      :list="list"
      :isCheckAll="isCheckAll"
      @delList="delList"
      @changeStatus="changeStatus"
      @changeChecked="changeChecked"
    ></ListBody>

    <!-- 底部部分 -->
    <ListFooter
      :numList="numList"
      @clearDoneList="clearDoneList"
      @toDoing="toDoing"
    ></ListFooter>
  </section>
</template>

<script>
import ListHeader from '@/components/ListHeader.vue'
import ListBody from '@/components/ListBody.vue'
import ListFooter from '@/components/ListFooter.vue'

export default {
  components: {
    ListHeader,
    ListBody,
    ListFooter
  },
  data () {
    return {
      inputList: '',
      // list: [
      //   { id: 1, name: '打豆豆', isDone: false },
      //   { id: 2, name: '烫头', isDone: true }
      // ]
      list: JSON.parse(localStorage.getItem('toDoList')) || [],

      listDoing: [],
    }
  },

  methods: {
    // 进行中列表 未完成·
    toDoing () {
      this.listDoing = this.list.filter(item => item.isDone === false)
      console.log(this.listDoing);
    },

    // 添加
    addList (value) {
      const obj = { id: +new Date(), name: value, isDone: false }
      this.list = [...this.list, obj]
      console.log(this.list)
    },

    //删除
    delList (id) {
      this.list = this.list.filter(item => {
        return item.id !== id
      })
    },

    // 改变状态
    changeStatus (id) {
      const res = this.list.find(item => {
        return item.id === id
      })
      res.isDone = !res.isDone
    },

    // 全选和反旋
    changeChecked (value) {
      this.list.forEach(item => {
        return (item.isDone = value)
      })
    },
    //清除所有已完成的事件
    clearDoneList () {
      this.list = this.list.filter(item => !item.isDone)
    }
  },
  computed: {
    //设置全选属性
    isCheckAll () {
      return this.list.every(item => item.isDone)
    },

    // 剩余未完成的数量
    numList () {
      return this.list.filter(item => item.isDone === false).length
    }
  },
  watch: {
    list: {
      immediate: true,
      deep: true,
      handler (value) {
        localStorage.setItem('toDoList', JSON.stringify(value))
      }
    }
  }
}
</script>

<style></style>
